//
//  WeiboView.m
//  微blog
//
//  Created by Jorn.Wu on 15/8/25.
//  Copyright (c) 2015年 Jorn.Wu(jorn_wza@sina.com). All rights reserved.
//

#import "WeiboView.h"
#import "HomeWeiboFrameModel.h"
#import "HomeWeiboModel.h"
#import "ThemeImageView.h"
#import "UIImageView+WebCache.h"
#import "WXLabel.h"
#import "ThemeManager.h"
#import "UserModel.h"
#import "ZoomImageView.h"

@interface WeiboView ()<WXLabelDelegate>

/**
 * 微薄文字
 */
@property (nonatomic, strong) WXLabel *textLabel;

/**
 * 原微博文字
 */
@property (nonatomic, strong) WXLabel *srLabel;

/**
 * 微博图片
 */
@property (nonatomic, strong) ZoomImageView *imageView;

/**
 * 源微博背景图片
 */
@property (nonatomic, strong) ThemeImageView *srBgImageView;

@end

@implementation WeiboView

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kThemeDidChangedNotification object:nil];
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        //响应主题切换通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:kThemeDidChangedNotification object:nil];
        
        //1 微博内容
        _textLabel = [[WXLabel alloc] initWithFrame:CGRectZero];
        _textLabel.numberOfLines = 0;
        _textLabel.linespace = 5;
        _textLabel.wxLabelDelegate = self;      //将自己作为代理，查找关键字
        _textLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:_textLabel];
        
        
        //2 原微博内容
        _srLabel = [[WXLabel alloc] initWithFrame:CGRectZero];
        _srLabel.hidden = YES;
        _srLabel.wxLabelDelegate = self;
        _srLabel.numberOfLines = 0;
        _srLabel.linespace = 5;
        _srLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:_srLabel];
        
        
        //3 图片
        _imageView = [[ZoomImageView alloc] initWithFrame:CGRectZero];
        _imageView.hidden = YES;
        [self addSubview:_imageView];
        
        
        //4 原微博背景图片
        _srBgImageView = [[ThemeImageView alloc] initWithFrame:CGRectZero];
        _srBgImageView.hidden = YES;
//        拉伸点设置
        _srBgImageView.leftCapWidth = 25;
        _srBgImageView.topCapWidth = 25;
        _srBgImageView.imageName = @"timeline_rt_border_9.png";
        
        //放在最低层
        [self insertSubview:_srBgImageView atIndex:0];
    
    }
    return self;
}

- (void)notificationAction:(NSNotification *)noti{
    
    ThemeManager *manager = [ThemeManager defualtManager];
    _textLabel.textColor = [manager getThemeColor:@"Timeline_Content_color"];
    _srLabel.textColor = [manager getThemeColor:@"Timeline_Retweet_color"];
}


/**
 * 传参时调用
 */
- (void)setWeiboFrameModel:(HomeWeiboFrameModel *)weiboFrameModel{
    
    if (_weiboFrameModel != weiboFrameModel) {
        
        _weiboFrameModel = weiboFrameModel;
        
        [self setNeedsLayout];
    }
   
}


/**
 * 根据weiboFrameModel布局子视图
 */
- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    HomeWeiboModel *weiboModel = self.weiboFrameModel.weiboModel;
    _textLabel.text = weiboModel.text;
    _textLabel.frame = self.weiboFrameModel.textFrame;
    
    if (weiboModel.srWeiboModel != nil) {       //是转发的微薄
        
        self.srBgImageView.hidden = NO;
        self.srBgImageView.frame = self.weiboFrameModel.bgImgFrame;
        
        
        self.srLabel.hidden = NO;
        /**
         * 加上源微博用户昵称(默认没有)
         */
        NSString *srUserName = weiboModel.srWeiboModel.userModel.screen_name;
        NSString *newSrWeiboText = [NSString stringWithFormat:@"@%@：%@",srUserName,weiboModel.srWeiboModel.text];
        self.srLabel.text = newSrWeiboText;
        self.srLabel.frame = self.weiboFrameModel.srTextFrame;
        
        NSString *imageUrl = weiboModel.srWeiboModel.thumbnailImage;
        NSString *fullImgUrl = weiboModel.srWeiboModel.originalImage;
        if (imageUrl != nil) {        //原微博有图片
            
            self.imageView.hidden = NO;
            self.imageView.frame = self.weiboFrameModel.imgFrame;
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
            
            self.imageView.fullImageUrlString = fullImgUrl;
  
        }else {         //原微博没有图片
            self.imageView.hidden = YES;
        }
        
    }else {         //是原创微薄
        
        self.srBgImageView.hidden = YES;
        self.srLabel.hidden = YES;
        
        NSString *imageUrl = weiboModel.thumbnailImage;
        if (!imageUrl) {        //没图片
            self.imageView.hidden = YES;
        }else {
            self.imageView.hidden = NO;
            self.imageView.frame = self.weiboFrameModel.imgFrame;
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
            
            //微博大图的链接
            self.imageView.fullImageUrlString = weiboModel.originalImage;
        }
        
    }
    
    
    /**
     * 判读是否为gif图片，添加图标
     */
    if (!self.imageView.hidden) {
        //有图片
        
        UIImageView *iconView = self.imageView.iconImageView;
        iconView.frame = CGRectMake(_imageView.width-24, _imageView.height-15, 24, 15);
        NSString *extension = [weiboModel.thumbnailImage pathExtension];
        
        if ([extension isEqualToString:@"gif"]) {
            iconView.hidden = NO;
            self.imageView.isGif = YES;
            
        } else {
            iconView.hidden = YES;
            self.imageView.isGif = NO;
            
        }
    }
    
}


#pragma mark - WXLabelDelegate


- (NSString *)contentsOfRegexStringWithWXLabel:(WXLabel *)wxLabel
{
    //需要添加链接字符串的正则表达式：@用户、http://、#话题#
    NSString *regex1 = @"@\\w+";
    NSString *regex2 = @"http(s)?://([A-Za-z0-9._-]+(/)?)*";
    NSString *regex3 = @"#\\w+#";
    NSString *regex = [NSString stringWithFormat:@"(%@)|(%@)|(%@)",regex1,regex2,regex3];
    return regex;
}

//手指离开当前超链接文本响应的协议方法
- (void)toucheEndWXLabel:(WXLabel *)wxLabel withContext:(NSString *)context{
    kSinaWeiboLog(@"%@",context);
}

//设置当前链接文本的颜色
- (UIColor *)linkColorWithWXLabel:(WXLabel *)wxLabel{
    return [[ThemeManager defualtManager] getThemeColor:@"Link_color"];
}
//设置当前文本点击时的颜色
- (UIColor *)passColorWithWXLabel:(WXLabel *)wxLabel{
    return [UIColor grayColor];
}


@end



















